<template>
  <div class="car">
   
    <!--  -->
    <van-checkbox-group
      v-model="result"
      v-for="item in cartList"
      :key="item.id"
    >
      <van-checkbox :name="item" @click="singleCheck">
        <van-card
          :num="item.number"
          :price="item.retail_price | formatMoney1"
          desc="描述信息"
          :title="item.goods_name"
          :thumb="item.list_pic_url"
        >
          <template #footer>
            <van-button size="mini"  @click.stop="deleteFn(item)"
              >删除</van-button
            >
          </template>
        </van-card>
      </van-checkbox>
    </van-checkbox-group>
    <!--  -->
    <van-submit-bar
      :price="totalPrice"
      button-text="提交订单"
      @submit="onSubmit"
    >
      <van-checkbox v-model="checked" @click="checkAll">全选</van-checkbox>
      <template #tip>
        你的收货地址不支持同城送,
        <span @click="onClickEditAddress">修改地址</span>
      </template>
    </van-submit-bar>
  </div>
</template>
<script>
//引入购物车数据
import { getCarData, deleteGoods, changeChecked } from "@/utils/http.js";
export default {
  name: "Car",
  data() {
    return {
      result: [], //选中的集合
      cartList: [], //购物车数组
      checked: false, // 全选状态
      totalPrice: 0, // 总价
      productIds: "",
    };
  },
  components: {},
  methods: {
    // 提交事件
    onSubmit() {},
    //   修改地址事件
    onClickEditAddress() {},
    // 全选事件
    checkAll() {
      // console.log(this.checked)
      let productIds = this.cartList.map((item) => {
        return item.product_id;
      });
     
      if (this.checked) {
        this.result = this.cartList;
      } else {
        this.result = [];
      }
      // console.log(this.result);
      let params = {
        isChecked: this.checked ? 1 : 0,
        productIds: productIds.join(","),
      };
      changeChecked(params).then((res) => {
        // console.log(res);
        // 商品总价赋值
        this.totalPrice = res.data.data.cartTotal.checkedGoodsAmount * 100;
      }).catch(e=>{})
    },
    // 单选事件
    singleCheck() {
      //   console.log(this.result);
      
      if (
        this.result.length == this.cartList.length &&
        this.cartList.length != 0
      ) {
        this.checked = true;
      } else {
        this.checked = false;
      }
    },
    // 删除
    deleteFn(item) {
      deleteGoods({
        productIds: String(item.product_id)
     
      }).then((res) => {
    console.log(res);
     // 重新渲染购物车数据
        this.cartList = res.data.data.cartList;
        // 重新计算商品总价赋值
        this.totalPrice = res.data.data.cartTotal.checkedGoodsAmount * 100;
        // 初始化单选状态
        this.result = []; // 清空
        this.cartList.forEach((item) => {
          if (item.checked == 1) {
            this.result.push(item);
          }
    this.gata()
    
      });
})

      
    },
    gata(){
      getCarData().then((res) => {
      // console.log(res);
      this.cartList = res.data.data.cartList;
      
    });
    },
     onClickLeft(){
    this.$router.go(-1)
  }
  },
  created() {
    // 请求购物车数据
    this.gata()
  },
};
</script>
<style scoped lang='less'>
.car{
  padding-bottom: 22%;
  /deep/.van-checkbox__label {
  flex: 1;
  
}
/deep/.van-submit-bar {
  position: fixed;
  bottom: 50px;
}

}

</style>